package com.burgeon.backend.college.client_side.rest;
/**
 * 获取符合条件的导购 用于生成企业微信员工
 */

import com.burgeon.backend.Rest.base.RESTBase;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 根据企业微信获取的userid即erp员工编号获取员工信息
 */
public class FindEmpNameById extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public FindEmpNameById(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     *
     * @return
     */

    public String getData(String userid) {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = null;
        try {
            str = RESULT(Data(userid), url);

            System.err.println(str);
            String code = new JSONArray(str).getJSONObject(0).getString("code");
            if (!"0".equals(code)) {
                throw new Exception();
            }

            JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");

            if (jsonArray.length() > 0) {

                return jsonArray.getJSONArray(0).getString(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private MultiValueMap Data(String userid) {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段


        columns.add("NAME");


        params.put("columns", columns);
        params.put("table", "C_V_EMPLOYEE");//表名

        params.put("count", "true");


        HashMap paramss = new HashMap();

        String ac = "'=" + userid + "'";
        paramss.put("column", "'ID'");
        paramss.put("condition", ac);


        params.put("params", paramss);

        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        FindEmpNameById g = new FindEmpNameById("8.130.161.41:55321", "nea@burgeon.com.cn", "burgeon55321");
        try {
            System.err.println(g.getData("2107"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
